<?php
namespace Administrator\Form;

use Zend\Form\Element;
use Zend\Form\Form;
use Zend\Form\Element\Csrf;
use Zend\Db\Adapter\AdapterInterface;

class LoginForm extends Form {
	protected $adapter;
    public function loginform($name=null) {
        parent::__construct($name);
        $this->setAttribute('method', 'post');
        //Khai báo kiểu dữ liệu được gửi lên server
        $this->setAttribute('enctype','multipart/form-data');
        
        $this->add(array(
            'name' => 'email',           
        		'attributes' => array(
        				'type' => 'text',
        				'class' => 'form-control',
        				'placeholder' => 'Nhập email',
        				'id' => 'email',
        				'required' => 'required',
        		),
        		'options' => array(
        				'disable_inarray_validator' => true,
        		)
        		
        ));      
        
       $this->add(array(
            'name' => 'password',          
       		'attributes' => array(
       				'type' => 'password',
       				'class' => 'form-control',
       				'placeholder' => 'Nhập mật khẩu',
       				'id' => 'password',
       				'required' => 'required',
       		),
       ));
       
       $this->add(array(
            'type' => 'Zend\Form\Element\Csrf',
            'name' => 'loginCsrf',
            'options' => array(
                'csrf_options' => array(
                    'timeout' => 3600
                )
            )
        ));
        
        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type' => 'submit',
                'value' => 'Đăng nhập',
            	'class' => 'btn btn-primary',
            	'id'  => 'login'
            ),
        ));
    }
    
    public function addresourceform($name=null,AdapterInterface $dbAdapter) {
    	$this->adapter =$dbAdapter;
    
    	$this->setAttribute('method', 'post');
    	//Khai báo kiểu dữ liệu được gửi lên server
    	$this->setAttribute('enctype','multipart/form-data');
    
    	$this->add(array(
    			'name' => 'name_resource',
    			'attributes' => array(
    					'type' => 'text',
    					'class' => 'form-control',
    					'placeholder' => 'Nhập Controller',
    					'id' => 'name_resource',
    					'required' => 'required',
    			),
    			'options' => array(
    					'disable_inarray_validator' => true,
    					'label' => 'Controller:',
    			)
    			 
    	));
    	
    	$this->add(array(
    			'name' => 'submit_resource',
    			'attributes' => array(
    					'type' => 'submit',
    					'value' => 'Lưu lại',
    					'class' => 'btn btn-primary',
    					'id'  => 'submit_resource'
    			),
    	));
    }
    
    public function minifyform($name=null) {
    
    	$this->setAttribute('method', 'post');
    	//Khai báo kiểu dữ liệu được gửi lên server
    	$this->setAttribute('enctype','multipart/form-data');
    	 
    	$this->add(array(
    			'type' => 'Zend\Form\Element\Select',
    			'name' => 'jsandcssfile',
    			'options' => array(
    					'disable_inarray_validator' => true,
    					'label' => 'Chọn file:',
    					'value_options' => $this->getfile(),
    					'required' => 'required',
    			),
    			'attributes' => array(
    					'class' => 'jsandcssfile form-control',
    					'multiple' =>true,
    					'size'=> 10
    			),
    	));
    	
    	$this->add(array(
    			'name' => 'minify',
    			'attributes' => array(
    					'type' => 'submit',
    					'value' => 'minify',
    					'class' => 'btn btn-primary',
    					'id'  => 'minify'
    			),
    	));
    }
    
    public function getfile(){
    	$arr = array();
    	$filebackend = scandir(TEMPALATE_PATH.'/backend/js');
    	foreach ($filebackend as $fileval)
    		if (( $fileval != '.' ) && ( $fileval != '..' ) && strpos( $fileval, ".js" ) == true && strpos( $fileval, ".min" ) == false)
    			array_push($arr,TEMPALATE_PATH.'/backend/js/'.$fileval);
    	
    		$filebackendcss = scandir(TEMPALATE_PATH.'/backend/css');
    		foreach ($filebackendcss as $fileval)
    			if (( $fileval != '.' ) && ( $fileval != '..' ) && strpos( $fileval, ".css" ) == true && strpos( $fileval, ".min" ) == false)
    				array_push($arr,TEMPALATE_PATH.'/backend/css/'.$fileval);
    		
    		$fileadministrator = scandir(TEMPALATE_PATH.'/administrator/js');
    		foreach ($fileadministrator as $fileval)
    			if (( $fileval != '.' ) && ( $fileval != '..' ) && strpos( $fileval, ".js" ) == true && strpos( $fileval, ".min" ) == false)
    				array_push($arr,TEMPALATE_PATH.'/administrator/js/'.$fileval);
    		
    			$fileadministratorcss = scandir(TEMPALATE_PATH.'/administrator/css');
    			foreach ($fileadministratorcss as $fileval)
    				if (( $fileval != '.' ) && ( $fileval != '..' ) && strpos( $fileval, ".css" ) == true && strpos( $fileval, ".min" ) == false)
    					array_push($arr,TEMPALATE_PATH.'/administrator/css/'.$fileval);
    			
    			$filejs = scandir(TEMPALATE_PATH.'/js');
    			foreach ($filejs as $fileval)
    				if (( $fileval != '.' ) && ( $fileval != '..' ) && strpos( $fileval, ".js" ) == true && strpos( $fileval, ".min" ) == false)
    					array_push($arr,TEMPALATE_PATH.'/js/'.$fileval);

    				$filecss = scandir(TEMPALATE_PATH.'/css');
    				foreach ($filecss as $fileval)
    					if (( $fileval != '.' ) && ( $fileval != '..' ) && strpos( $fileval, ".css" ) == true && strpos( $fileval, ".min" ) == false)
    						array_push($arr,TEMPALATE_PATH.'/css/'.$fileval);
    				
    				$filefrontend = scandir(TEMPALATE_PATH.'/frontend/js');
    				foreach ($filefrontend as $fileval)
    					if (( $fileval != '.' ) && ( $fileval != '..' ) && strpos( $fileval, ".js" ) == true && strpos( $fileval, ".min" ) == false)
    						array_push($arr,TEMPALATE_PATH.'/frontend/js/'.$fileval);
    					
    					$filefrontendcss = scandir(TEMPALATE_PATH.'/frontend/css');
    					foreach ($filefrontendcss as $fileval)
    						if (( $fileval != '.' ) && ( $fileval != '..' ) && strpos( $fileval, ".css" ) == true && strpos( $fileval, ".min" ) == false)
    							array_push($arr,TEMPALATE_PATH.'/frontend/css/'.$fileval);
		      $arrnew = array();
		      foreach ($arr as $key=>$val)
		      	$arrnew[$val] = $val;

		    	return $arrnew;
    }
    
    public function userform($name=null,AdapterInterface $dbAdapter) {
    	$this->adapter =$dbAdapter;

    	$this->setAttribute('method', 'post');
    	//Khai báo kiểu dữ liệu được gửi lên server
    	$this->setAttribute('enctype','multipart/form-data');    
    
    	$this->add(array(
    			'type' => 'Zend\Form\Element\Select',
    			'name' => 'vaitro_user',
    			'options' => array(
    					'disable_inarray_validator' => false,
    					'label' => 'Vai trò User:',
    					'empty_option' => '--Chọn Vai trò--',
    					'value_options' =>$this->getOptionsForSelectrole(),
    					'autocomplete' => 'off',
    					'inarrayvalidator' => false,
    					'required' => 'required',
    			),
    			'attributes' => array(
    					'class' => 'form-control vaitro_user',
    			),
    	));
    	 
    	$this->add(array(
    			'name' => 'email_user',
    			'attributes' => array(
    					'type' => 'text',
    					'class' => 'form-control',
    					'placeholder' => 'Nhập email',
    					'id' => 'email_user',
    					'required' => 'required',
    			),
    			'options' => array(
    					'disable_inarray_validator' => true,
    					'label' => 'Email:',
    			)
    			 
    	));
    	 
    	$this->add(array(
    			'name' => 'name_user',
    			'attributes' => array(
    					'type' => 'text',
    					'class' => 'form-control',
    					'placeholder' => 'Nhập tên',
    					'id' => 'name_user',
    					'required' => 'required',
    			),
    			'options' => array(
    					'disable_inarray_validator' => true,
    					'label' => 'Tên User:',
    			)
    
    	));
    	 
    	$this->add(array(
    			'name' => 'password_user',
    			'attributes' => array(
    					'type' => 'password',
    					'class' => 'form-control',
    					'placeholder' => 'Nhập mật khẩu',
    					'id' => 'password_user',
    					'required' => 'required',
    			),
    			'options' => array(
    					'label' => 'Mật khẩu:',
    			)
    	));
    	 
    	$this->add(array(
    			'name' => 'password_user_edit',
    			'attributes' => array(
    					'type' => 'password',
    					'class' => 'form-control',
    					'placeholder' => 'Nhập mật khẩu',
    					'id' => 'password_user_edit',
    			),
    			'options' => array(
    					'label' => 'Mật khẩu:',
    			)
    	));
    	
    	$this->add(array(
    			'type' => 'Zend\Form\Element\Checkbox',
    			'name' => 'active_user',
    			'options' => array(
    					'label' => 'Active:',
    					'checked_value' => '1',
    					'unchecked_value' => '0',
    			),
    			'attributes' => array(
    					'id' => 'active_user'
    			)
    	));
    
    	$this->add(array(
    			'name' => 'submit_user',
    			'attributes' => array(
    					'type' => 'submit',
    					'value' => 'Lưu lại',
    					'class' => 'btn btn-primary',
    					'id'  => 'submit_user'
    			),
    	));
    	
    	$this->add(array(
    			'name' => 'submit_user_edit',
    			'attributes' => array(
    					'type' => 'submit',
    					'value' => 'Lưu lại',
    					'class' => 'btn btn-primary',
    					'id'  => 'submit_user_edit'
    			),
    	));
    }
    
    public function getOptionsForSelectrole()
    {
    	$dbAdapter = $this->adapter;
    	$sql       = 'SELECT rid,role_name  FROM role ORDER BY rid ASC';
    	$statement = $dbAdapter->query($sql);
    	$result    = $statement->execute();
    
    	$selectData = array();
    
    	foreach ($result as $res) {
    		$selectData[$res['rid']] = $res['role_name'];
    	}
    	return $selectData;
    }
}